if(!com) var com = {};
if(!com.imgurex) com.imgurex = {};
if(!com.imgurex.imgur_fox) com.imgurex.imgur_fox = {};


function ReplaceAll(Source,stringToFind,stringToReplace){
  var temp = Source;
  var index = temp.indexOf(stringToFind);
  while(index != -1){
    temp = temp.replace(stringToFind,stringToReplace);
    index = temp.indexOf(stringToFind);
  }
  return temp;
}




com.imgurex.imgur_fox = {

    imgur_URL: "http://imgur.com",
    targetURL: '',

    copyToClip: function(url) {
        // Copy image location to clipboard
        var img_location = url;
        const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);  
        gClipboardHelper.copyString(img_location); 
        
    },

    showImageContext: function() {
        var imgur_fox = com.imgurex.imgur_fox;

        document.getElementById("context-imgurex-search").hidden = true;

        if(gContextMenu.onImage) {
            imgur_fox.targetURL = gContextMenu.mediaURL;
            // For versions of Firefox < 3.1
            if(!imgur_fox.targetURL) {
                imgur_fox.targetURL = gContextMenu.imageURL;
            }
            document.getElementById("context-imgurex-search").hidden = false;
        }
    },

    showImageInit: function() {
        var menu = document.getElementById("contentAreaContextMenu");
        menu.addEventListener("popupshowing", com.imgurex.imgur_fox.showImageContext, false);
    },

    imgurSearch: function() {
        var imgur_fox = com.imgurex.imgur_fox;

        if(!imgur_fox.targetURL) return;
        //var location = imgur_fox.imgur_URL + '/processUpload1.php?&url=' + encodeURIComponent(imgur_fox.targetURL);
   var ret = true;
   var obj;
    var imgURI = {};
    var imgDelURI = {};
    var prefManager;
  try {
    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    prefManager = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
    const cid = "@imgur.com/imgurUploaderCOM;1";
    obj = Components.classes[cid].createInstance();  
    obj = obj.QueryInterface(Components.interfaces.IImgurUploaderCOM);
    var upURI = imgur_fox.targetURL;
    var upMeth;
    if(upURI.substring(0, 7) == "http://")
      upMeth = 1;
    else if (upURI.substring(0, 7) == "file://")
    {
     upMeth = 2;
     
     upURI = upURI.substring(8, upURI.length);
     /* TODO: convert file:// uri to local path */
     upURI = ReplaceAll(upURI, "/", "\\");
     upURI = ReplaceAll(upURI, "%20", " ");
     
    }
    else upMeth = 0; // error
    if(upMeth>0)
    {
      ret = obj.uploadImage(upMeth, upURI, imgURI, imgDelURI);
      if(ret != 0) return;
    } else return;
  }
  catch(err)
  {
  alert(err);
    //alert(err);
    //ret = false;
    return;
  }    
        //open new tab and when finished loading copy url to clipboard
        var postUp = imgURI.value.replace(imgur_fox.imgur_URL, "");
        postUp = postUp.substr(0, postUp.indexOf("."));
      
//*
// taken from http://blog.mozilla.com/addons/2009/01/28/how-to-develop-a-firefox-extension/
				var copyToClip = prefManager.getBoolPref("extensions.imgruploaderex.copyurltoclip");
				var openNewTab = prefManager.getBoolPref("extensions.imgruploaderex.opennewtab");
//*/



        // if we want
        if(openNewTab) {
          gBrowser.getBrowserForTab(gBrowser.addTab(imgur_fox.imgur_URL + postUp + "?tags"));
        }
       // if we want
       if(copyToClip) {
         if(!openNewTab) alert("Image uploaded!");
         imgur_fox.copyToClip(imgURI.value);
       }

        
        imgur_fox.targetURL = ''; // Reset

    }

}

window.addEventListener("load", com.imgurex.imgur_fox.showImageInit, false);
